#ifndef EPUBDUKPT_COMM_MUTEX_HPP
#define EPUBDUKPT_COMM_MUTEX_HPP

SJR_NAMESPACE_BEGIN

class EpubMutex final {
public:
  EpubMutex() : mMutex() {}

  EpubMutex(EpubMutex const &) = delete;
  EpubMutex &operator=(EpubMutex const &) = delete;

  void lock() {
    mMutex.lock();
  }

  void unlock() {
    mMutex.unlock();
  }

private:
  std::mutex mMutex;
};

class EpubUniqueLock final {
public:
  explicit EpubUniqueLock(EpubMutex &mutex) : mMark(), mMutex(mutex) {
    mMark = true;
    mMutex.lock();
  }

  ~EpubUniqueLock() {
    if (mMark)
      mMutex.unlock();
  }

  EpubUniqueLock(EpubUniqueLock const &) = delete;
  EpubUniqueLock &operator=(EpubUniqueLock const &) = delete;

private:
  bool mMark;
  EpubMutex &mMutex;
};

SJR_NAMESPACE_END

#endif//EPUBDUKPT_COMM_MUTEX_HPP
